/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hitechbangla.fctu.form;

import com.hitechbangla.fctu.base.GameScene;
import com.hitechbangla.fctu.base.PressedButtonEnum;
import com.hitechbangla.fctu.base.aircraft.Aircraft;
import com.hitechbangla.fctu.utility.GeneralUtil;
import com.hitechbangla.fctu.utility.RecordedGame;
import fctu.simulator.HTB_MAIN;
import java.awt.Window.Type;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.stream.FileImageInputStream;
import javax.swing.JFileChooser;

import com.hitechbangla.fctu.utility.MultipleAircraft;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import javax.swing.JOptionPane;

public class Utility extends javax.swing.JInternalFrame 
{
    private RecordedGame recordedGame;
    private int count;    
    private int measureButtonClick=0;
    
    private MultipleAircraft mac;
    private GameScene gameScene;

    private void setAlwaysOnTop(boolean b) {
        //throw new UnsupportedOperationException("Not yet implemented");
    }

    private void setType(Type type) {
        //throw new UnsupportedOperationException("Not yet implemented");
    }
    
    class RecordedGameTask extends TimerTask
    {
	@Override
	public void run() 
	{
	    loadGameScene(recordedGame.getAllGameScene().get(count));
	    ////System.out.println("from utility: count " + count);
	    count++;
	}	
    }
    
    private void loadGameScene(GameScene gameScene)
    {
	htbParent.frameGameControl.gamePauseNow();
	this.htbParent.gameScene = gameScene;	
	gameScene.HTBParent = htbParent;
	htbParent.frameGameControl.gameStartNow();
    }
    
    HTB_MAIN htbParent;
    
    public Utility(HTB_MAIN htbParent) 
    {
        super("Utility", true, true, true, true);
        this.htbParent = htbParent;        
        initComponents();	
	count=0;
    }
    
    public void Open()
    {
        this.setVisible(true);
    }
    
    public void Close()
    {
       // this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setVisible(false);
        dispose();    
    }   
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton5 = new javax.swing.JButton();
        jButtonRecordGame = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButtonPlayRecordedGame = new javax.swing.JButton();
        jButtonChangeTypes = new javax.swing.JButton();
        jButtonRandomAircraft = new javax.swing.JButton();

        setTitle("Utility");

        jButton5.setText("Measure");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButtonRecordGame.setText("Record Game");
        jButtonRecordGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRecordGameActionPerformed(evt);
            }
        });

        jButton6.setText("Draw Tool");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButtonPlayRecordedGame.setText("Play Recorded Game");
        jButtonPlayRecordedGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPlayRecordedGameActionPerformed(evt);
            }
        });

        jButtonChangeTypes.setText("Change Types");
        jButtonChangeTypes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonChangeTypesActionPerformed(evt);
            }
        });

        jButtonRandomAircraft.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButtonRandomAircraft.setText("Randomly Generated Aircraft");
        jButtonRandomAircraft.setMargin(new java.awt.Insets(2, 10, 2, 10));
        jButtonRandomAircraft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRandomAircraftActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonRecordGame, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonPlayRecordedGame)
                    .addComponent(jButtonChangeTypes, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonRandomAircraft, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(jButtonRecordGame, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jButtonPlayRecordedGame, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(jButtonChangeTypes, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonRandomAircraft, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // measure button click action 
        //htbParent.lastPressedButton = PressedButtonEnum.NONE;
        if (measureButtonClick == 0) {
	    measureButtonClick = 1;
	} else if (measureButtonClick == 1) {
	    measureButtonClick = 0;
	}

       // htbParent.lastPressedButton = PressedButtonEnum.MEASURE_TOOL;
        
        if(measureButtonClick==1)
        {
            htbParent.lastPressedButton = PressedButtonEnum.MEASURE_TOOL;
        }else
        {
            htbParent.lastPressedButton = PressedButtonEnum.NONE;
        }
        
        ////System.out.println("from utility Measure Helper ");
        //htbParent.controlMeasureClick();
        
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButtonRecordGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRecordGameActionPerformed
        Record record = new Record(htbParent);
	record.setVisible(true);
    }//GEN-LAST:event_jButtonRecordGameActionPerformed

    private void jButtonPlayRecordedGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPlayRecordedGameActionPerformed
	
	JFileChooser jFileChooser = new JFileChooser();
	int returnVal = jFileChooser.showOpenDialog(this);
	
	if(returnVal == JFileChooser.APPROVE_OPTION)
	{
	    File file = jFileChooser.getSelectedFile();	    
	    
	    try
	    {
		//file.createNewFile();		
		
		FileInputStream fileInputStream = new FileInputStream(file);
		ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
		this.recordedGame = (RecordedGame)objectInputStream.readObject();		
		objectInputStream.close();				
		
		////System.out.println("from utility: size" + this.recordedGame.size());
		
		for(Long time:this.recordedGame.getAllTime())
		{
		    //System.out.println("time:" + time);
		}		    
		
		Timer timer = new Timer();		
		
		for(int i=0;i<recordedGame.size();i++)
		{
		    timer.schedule(new RecordedGameTask(), recordedGame.getAllTime().get(i));
		}	
		
	    }	    
	    catch(Exception ex)
	    {
		//System.out.println("from record:" + ex.toString());
	    }    
	    
	    //System.out.println("from record: file exists " + file.exists());	    
	    //System.out.println(file.getAbsolutePath());
	}
    }//GEN-LAST:event_jButtonPlayRecordedGameActionPerformed

    private void jButtonChangeTypesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonChangeTypesActionPerformed
        ChangeTypes changeTypes = new ChangeTypes();
	changeTypes.setVisible(true);
    }//GEN-LAST:event_jButtonChangeTypesActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        DrawingToolsForm form = new DrawingToolsForm();
        htbParent.lastPressedButton = PressedButtonEnum.DRAW_TOOLS;
        form.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButtonRandomAircraftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRandomAircraftActionPerformed
        
        //Rectangle2D.Double rectangle = new Rectangle2D.Double(-174.48486328125,84.97376365588997, 2, 25);
        Rectangle2D.Double rectangle = new Rectangle2D.Double(-162.59765625,81.91082826021776,22, 3);
        MultipleAircraft multipleAircraft = new MultipleAircraft(rectangle);
        
        
        for(Aircraft aircraft:multipleAircraft.getMultipleAircraftList())
        {
            HTB_MAIN.gameScene.airCraftList.add(aircraft);
        }
        
        /*
        System.out.println("mac");
        for(int i=0;i<mac.getMultipleAircraftList().size();i++)
        {
            Aircraft ac= mac.getMultipleAircraftList().get(i);
            gameScene.macList.add(ac);
            gameScene.HTBParent.repaint();
            this.setVisible(false);
            dispose();
            
            GeneralUtil.UpdateGameScene(gameScene);
	    
	    RecordedGame recordedGame = gameScene.HTBParent.getRecordedGame();
	    
	    if(recordedGame!=null)
	    {
		if(recordedGame.isRecordingStarted())
		{
		    recordedGame.storeGameScene(gameScene);
		}
	    }	
        }
        */
    }//GEN-LAST:event_jButtonRandomAircraftActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Utility.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Utility.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Utility.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Utility.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new Utility().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButtonChangeTypes;
    private javax.swing.JButton jButtonPlayRecordedGame;
    private javax.swing.JButton jButtonRandomAircraft;
    private javax.swing.JButton jButtonRecordGame;
    // End of variables declaration//GEN-END:variables
}
